// DlgPumpList.cpp : implementation file
//

#include "stdafx.h"
#include "pdarx.h"
#include "DlgPumpList.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDlgPumpList dialog


CDlgPumpList::CDlgPumpList(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgPumpList::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgPumpList)
	m_strSql = _T("");
	m_strSpec = _T("");
	m_strModel = _T("");
	m_strTableName = _T("");
	//}}AFX_DATA_INIT
	::AfxSetResourceHandle(g_ARXInstance);
}


void CDlgPumpList::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgPumpList)
	DDX_Control(pDX, IDC_LIST1, m_list);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgPumpList, CDialog)
	//{{AFX_MSG_MAP(CDlgPumpList)
	ON_WM_CREATE()
	ON_NOTIFY(NM_DBLCLK, IDC_LIST1, OnDblclkList1)
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST1, OnItemchangedList1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgPumpList message handlers

BOOL CDlgPumpList::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	CString strVal;

	m_list.InsertColumn(0, _T("FRAME"),LVCFMT_LEFT, 150);
	m_list.InsertColumn(1, _T("INLET_SIZE"), LVCFMT_LEFT, 150);
	m_list.InsertColumn(2, _T("DISCHARGE_SIZE"), LVCFMT_LEFT, 150);
	m_list.InsertColumn(3, _T("LONG DESCR"), LVCFMT_LEFT, 500);

	DWORD styles = m_list.GetExtendedStyle();
	m_list.SetExtendedStyle(styles|LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);

	_variant_t var;
	_RecordsetPtr pRs;

	m_strSql.Format(_T("select * from %s where SPEC = '%s' and MODEL = '%s'"),m_strTableName, m_strSpec, m_strModel); 
	try
	{
		pRs.CreateInstance(__uuidof(Recordset));
		pRs->Open(_variant_t(m_strSql),(IDispatch *)g_pConPump,adOpenStatic,adLockOptimistic,adCmdText);
	}
	catch(_com_error e)
	{
		AfxMessageBox(e.ErrorMessage());
		return FALSE;	
	}
	int i = 0;
	if(pRs->GetRecordCount() > 0)
	{
		pRs->MoveFirst();
		while(!pRs->adoEOF)
		{
			var = pRs->GetCollect(_variant_t(_T("FRAME")));
			if(var.vt != VT_NULL)
			{
				strVal = V_BSTR(&var);
				m_list.InsertItem(i, strVal);
			}
			var = pRs->GetCollect(_variant_t(_T("INLET_SIZE")));
			if(var.vt != VT_NULL)
			{
				strVal.Format(_T("%.2f"), static_cast<float>(var));
				m_list.SetItemText(i, 1, strVal);
			}
			var = pRs->GetCollect(_variant_t(_T("DISCHARGE_SIZE")));
			if(var.vt != VT_NULL)
			{
				strVal.Format(_T("%.2f"), static_cast<float>(var));
				m_list.SetItemText(i, 2, strVal);
			}
			var = pRs->GetCollect(_variant_t(_T("LONG_DESCR")));
			if(var.vt != VT_NULL)
			{
				strVal = V_BSTR(&var);
				m_list.SetItemText(i, 3, strVal);
			}
			i++;
			pRs->MoveNext();
		}
	}
	pRs->Close();
	m_list.SetHotItem(0);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

int CDlgPumpList::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	::AfxSetResourceHandle(g_CADInstance);
	
	return 0;
}

void CDlgPumpList::OnDblclkList1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	OnOK();
	
	*pResult = 0;
}

void CDlgPumpList::OnOK() 
{
	int item = m_list.GetHotItem();
	m_strFrame = m_list.GetItemText(item, 0); 
	CDialog::OnOK();
}



void CDlgPumpList::OnItemchangedList1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
	
	m_list.SetHotItem(pNMListView->iItem);
	
	*pResult = 0;
}
